
package com.hlkj.pay.app.merchant.ext.impl.hnapay.handler;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.IMerchantAccessAppService;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.constant.HnapayConstant;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayConfig;
import com.hlkj.pay.common.PayResultCode;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import net.dreamlu.mica.core.utils.StringPool;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * 易生 商户进件参数处理
 * 
 * @author HlpayTeam
 * @date 2024/09/26 12:02
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HnapayMerchantCommonParamsHandle {

    private final IMerchantAccessAppService merchantAccessAppService;

    //获取aut子商户号
    public MerchantAccessAuthDO randomMchId(String channelCode, PayEnums.PAY_TYPE payType, HnapayAppChannelParams appChannelParams) {
//        if ("WECHAT".equals(payType.getType())) {
//            if (StringUtils.hasText(appChannelParams.getWxMerchantId())) {
//                MerchantAccessAuthDO authDO = new MerchantAccessAuthDO();
//                authDO.setSerialCode(appChannelParams.getAliMerchantId());
//                return authDO;
//            }
//        } else {
//            if (StringUtils.hasText(appChannelParams.getAliMerchantId())) {
//                MerchantAccessAuthDO authDO = new MerchantAccessAuthDO();
//                authDO.setSerialCode(appChannelParams.getAliMerchantId());
//                return authDO;
//            }
//        }
        return merchantAccessAppService.randomMchIdByChannelCode(channelCode,payType);
    }

   public CommonResult<HnapayConfig> channelConfig(ProviderChannelConfigDO providerChannelConfigDO) {
        // 移除config 不需要的 属性
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        Map<String, String> paramsConfigValue = JsonUtils.parseObject(channelConfig, Map.class);
        HnapayConfig bean = new HnapayConfig();
        Map<String, Object> objectMap = BeanUtil.toMap(bean);
        Set<String> keys = objectMap.keySet();
        // 使用 keySet() 过滤 Map
        paramsConfigValue.keySet().removeIf(key -> !keys.contains(key));
        channelConfig = JsonUtils.toJsonString(paramsConfigValue);
        providerChannelConfigDO.setParamsConfig(channelConfig);
        // 配置转换
        try {
            HnapayConfig easyPayConfig = JsonUtils.parseObject(channelConfig, HnapayConfig.class);

            return CommonResult.success(easyPayConfig);
        } catch (Exception e) {
            return CommonResult.error(PayResultCode.PAY_CHANNEL_CONFIG_ERROR.getCode(), "新生渠道参数配置格式错误请求失败,失败信息" + e.getMessage());
        }
    }

    public static String date(String date) {
        if (!StringUtils.hasText(date)) {
            return null;
        }
        return date.replace(StringPool.DASH, StringPool.EMPTY);
    }

    public static String domain(boolean test) {

        return HnapayConstant.DOMAIN_PROD;
    }
}
